package com.run.control;
import java.util.HashMap;
import java.sql.SQLException;

import com.ar.comp.db.Mysql;

import com.ar.core.Ar;
import com.ar.core.DbException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping(path="/start") // This means URL's start with /demo (after Application path)
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping(path="/add") // Map ONLY POST Requests
  public @ResponseBody String addNewUser (@RequestParam String name
      , @RequestParam String email) {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User();
    n.setName(name);
    n.setEmail(email);
    userRepository.save(n);
    return "Saved";
  }


  @GetMapping(path="/arws") // Map ONLY POST Requests
  public @ResponseBody void startAwServer (@RequestParam String ws
          ) {
    Ar.run();
  }

  @PostMapping(path="/arws") // Map ONLY POST Requests
  public @ResponseBody void startAwServerP (@RequestParam String ws
  ) {
    Ar.run();
  }

  @Autowired
  private JdbcTemplate jdbcTemplate2;
  @GetMapping(path="/all")
  public @ResponseBody Iterable<User> getAllUsers() {
    // This returns a JSON or XML with the users


    SimpleDriverDataSource ds = new SimpleDriverDataSource();
    try {
        ds.setDriver(new com.mysql.jdbc.Driver());
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ds.setUrl("jdbc:mysql://192.168.101.129:3306/db_example");
    ds.setUsername("root");
    ds.setPassword("cdc2021");

    String sql = "SELECT COUNT(*) FROM user";


    

    JdbcTemplate jtm = new JdbcTemplate(ds);
    int numOfCars = jtm.queryForObject(sql, Integer.class);

    int i2 = jdbcTemplate2.queryForObject(sql, Integer.class);


    // Db db = new Db();

    // JdbcTemplate ts4 = db.read("default");
    // int i4 = ts4.queryForObject(sql, Integer.class);

    HashMap<String, Object> conds = new HashMap<>();
    conds.put("id > ", 0);

//    int i3 = new Mysql().read("default").table("user").select("count(*) as t").where(conds).queryInt();
//    System.out.format("count t:  %d  in the table", i3);
//    JSONArray res = new Mysql().read("default").table("user").select("*").where(conds).queryAll("");

//    System.out.println(res.toString());

//    conds.put("id", 2);

    JSONObject jsonObject = new Mysql().read("default").table("user").select("*").where(conds).queryRow("");

    System.out.println(jsonObject);

    HashMap<String, Object> insertData = new HashMap<>();
    insertData.put("email", "yy");

    long insert = new Mysql().read("default").table("user").insert(insertData);
    System.out.println(insert);

    HashMap<String, Object> updateData = new HashMap<>();
    updateData.put("name", "hhh");

//    conds.put("id", 3);
    int update = 0;
    try {
      update = new Mysql().read("default").where(conds).table("user").update(updateData);
    } catch (DbException e) {
      e.printStackTrace();
    }
    System.out.println(update);



    int delete = 0;
    try {
      conds.put("id > ", 50);
      delete = new Mysql().read("default").where(conds).table("user").delete();
    } catch (DbException e) {
      e.printStackTrace();
    }
    System.out.println(delete);

//    SqlRowSet row3 = new Mysql().read("default").table("user").select("id").where(conds).queryRow();
//
//
//    System.out.format("There are %d cars in the table", numOfCars);
//    System.out.format("There are %d oi in the table", i2);











    return userRepository.findAll();
  }
}